define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/shoptableorder/index' + location.search,
                    add_url: 'order/shoptableorder/add',
                    edit_url: 'order/shoptableorder/edit',
                    del_url: 'order/shoptableorder/del',
                    multi_url: 'order/shoptableorder/multi',
                    import_url: 'order/shoptableorder/import',
                    table: 'shop_table_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                singleSelect: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'shop_name', title: __('店铺名字'), operate: 'LIKE', align: 'left',
                            formatter: function (value, row, index, field) {
                                if (value == null) {
                                    return "";
                                }
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + row.shop_name + "'>" + value + "</span>";
                            },
                            cellStyle: function (value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "170px"
                                    }
                                };
                            }
                        },
                        {field: 'table_name', title: __('台桌'), operate: 'LIKE'},
                        {
                            field: 'create_time',
                            title: __('时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'status',
                            title: __('状态'),
                            searchList: {
                                "payed": __('已支付'),
                                "doing": __('进行中'),
                                "done": __('已完成'),
                                "refund": __('已退款')
                            },
                            formatter: Table.api.formatter.status
                        },
                        {field: 'order_no', title: __('订单号'), operate: 'LIKE'},
                        {field: 'amount', title: __('金额'), operate: false},
                        {field: 'pay_amount', title: __('实付金额'), operate: false},
                        {field: 'ya_amount', title: __('押金'), operate: false},
                        {field: 'balance', title: __('余额'), operate: false},
                        {field: 'refund_amount', title: __('退款金额'), operate: false},
                        {field: 'mt_code', title: __('美团券'), operate: false},
                        {field: 'dou_code', title: __('抖音券'), operate: false},
                        {field: 'user.mobile', title: __('下单人手机号')},
                        {field: 'book_start', title: __('开始时间'), formatter: Table.api.formatter.datetime},
                        {field: 'book_end', title: __('结束时间'), formatter: Table.api.formatter.datetime},

                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'list',
                                    title: __('退款'),
                                    text: '退款',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "order/shoptableorder/add?id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('开台'),
                                    text: '开台',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    confirm: '是否开台',
                                    url: function (row) {
                                        return "order/shoptableorder/open?id=" + row['id'];
                                    },
                                    refresh: true,
                                },
                                {
                                    name: 'list',
                                    title: __('关台'),
                                    text: '关台',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    confirm: '是否关闭台桌',
                                    url: function (row) {
                                        return "order/shoptableorder/close?id=" + row['id'];
                                    },
                                    refresh: true,
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
